<template>
	<view class="page">
		<image class="img" src="../../static/bg.png"></image>
		<view class="logo">
			<image src="../../static/logo.png"></image>
		</view>
		<view class="but">
			<view :class="active===0? 'butcode':''" @click="active=0">验证码登录</view>
			<view :class="active===1? 'butphone':''" @click="active=1" style="margin-left:44rpx;">账号密码登录</view>
		</view>
		<view class="form">
			<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
			<u--form labelPosition="left" labelWidth="70" ref="uForm">
				<u-form-item class="phone" label="手机号" prop="params.phone">
					<u-input v-model="params.phone" :maxlength="11" fontSize="12" placeholder="请输入手机号">
						<template slot="prefix">
							<u-line margin="0rpx 10rpx 0rpx 0rpx" color="#FCD642" length="20" direction="col"></u-line>
						</template>
					</u-input>
				</u-form-item>
				<u-form-item v-if="active===1" label="密码" prop="params.password">
					<u-input password v-model="params.password" fontSize="12" placeholder="请输入密码">
						<template slot="prefix">
							<u-line margin="0rpx 10rpx 0rpx 0rpx" color="#FCD642" length="20" direction="col"></u-line>
						</template>
					</u-input>
				</u-form-item>
				<u-form-item v-else label="验证码" prop="params.code" ref="item1">
					<u-input v-model="params.code" fontSize="12" placeholder="请输入验证码">
						<template slot="prefix">
							<u-line margin="0rpx 10rpx 0rpx 0rpx" color="#FCD642" length="20" direction="col"></u-line>
						</template>
						<template slot="suffix">
							<u-toast ref="uToast"></u-toast>
							<u-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒重新获取"></u-code>
							<view style="color:#0F62F0;font-size: 26rpx;" @tap="getCode">{{tips}}</view>
						</template>
					</u-input>
				</u-form-item>

			</u--form>
			<!-- 			<view class="amend" v-if="active===1">
				<view @click="amend">修改密码</view>
				<view>忘记密码</view>
			</view> -->
			<view class="checkbox">
				<view style="display: flex;">

					<u-checkbox-group placement="column" @change="read">
						<u-checkbox :checked="che" :name="1" activeColor="#FBCC32" shape="circle">
						</u-checkbox>
					</u-checkbox-group>
					<view>登录即代表您同意<text style="color:#0F62F0">《用户协议》</text>和<text style="color:#0F62F0"
							@click="$goBack(2,'/bundle/pages/privacyagreement/privacyagreement')">《隐私政策》</text>，未注
					</view>
				</view>
				<view>册绑定的手机号验证成功后将自动注册</view>
			</view>
			<u-button iconColor="#FBCC32" @click="submit">登录/注册</u-button>
		</view>
		<u-modal :show="show" :content='content' @cancel="show=false" :title="title" :showCancelButton="true"
			@confirm="confirm"></u-modal>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		apilogin,
		apiccode
	} from "@/api/app.js"
	export default {
		data() {
			return {

				show: false,
				title: '用户协议与隐私政策',
				che: false,
				code: '发送验证码',
				active: 0,
				tips: 0,
				params: {
					phone: '',
					code: '',
					password: ''
				},
				content: `为了保证与你相关合法权益，请阅读并同意用户协议和隐私政策
				`
			}
		},
		created() {},
		methods: {
			read(n) {
				if (n.length === 1) {
					this.che = true;
				} else {
					this.che = false;
				}
			},
			amend() {
				uni.navigateTo({
					url: '/pages/amend/amend'
				})
			},
			confirm() {
				this.che = true
				this.submit()
			},
			submit() {
				let phone = this.params.phone.replace(/(^\s*)|(\s*$)/g, ''); //去除空格;
				if (phone == '' || phone == undefined || phone == null) {
					this.$refs.uToast.show({
						type: 'default',
						message: "请输入手机号"
					})
					return false;
				}
				if (this.active === 0) {
					let code = this.params.code.replace(/(^\s*)|(\s*$)/g, ''); //去除空格;
					if (code == '' || code == undefined || code == null) {
						this.$refs.uToast.show({
							type: 'default',
							message: "请输入验证码"
						})
						return false;
					}
					if (!this.che) {
						this.show = true
						return false
					} else {
						this.show = false
					}

					let param = {
						grantType: 'wxPhoneCaptcha',
						account: this.params.phone,
						tenantId: '000000',
						utf: 1,
					}
					apilogin(param).then((res) => {
						if (res.code === 200) {
							uni.setStorageSync('blade-auth', res.data.refreshToken)
							uni.setStorageSync('type', res.data.type)
							uni.setStorageSync("userId", res.data.userId)
							this.$refs.uToast.show({
								type: 'default',
								message: "登录成功"
							})
							if (res.data.type == 2) {
								setTimeout(() => {
									uni.redirectTo({
										url: "/pages/home/home"
									})
								}, 1000)
							} else {
								setTimeout(() => {
									uni.redirectTo({
										url: "/pages/homeBoss/homeBoss"
									})
								}, 1000)
							}
						}
					})

				} else {
					let password = this.params.password.replace(/(^\s*)|(\s*$)/g, ''); //去除空格;
					if (password == '' || password == undefined || password == null) {
						this.$refs.uToast.show({
							type: 'default',
							message: "请输入密码"
						})
						return false;
					}
					if (!this.che) {
						this.show = true
						return false
					} else {
						this.show = false
					}

					let param = {
						grantType: 'wxPhonePass',
						account: this.params.phone,
						tenantId: '000000',
						password: this.params.password,
						utf: 1,

					}
					apilogin(param).then((res) => {
						if (res.code === 200) {
							uni.setStorageSync('blade-auth', res.data.refreshToken)
							uni.setStorageSync('type', res.data.type)
							uni.setStorageSync("userId", res.data.userId)

							this.$refs.uToast.show({
								type: 'default',
								message: "登录成功"
							})

							if (res.data.type == 2) {
								setTimeout(() => {
									uni.redirectTo({
										url: "/pages/home/home"
									})
								}, 1000)
							} else {
								setTimeout(() => {
									uni.redirectTo({
										url: "/pages/homeBoss/homeBoss"
									})
								}, 1000)
							}
						}
					})
				}

			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
				if (!reg_tel.test(this.params.phone)) {
					this.$refs.uToast.show({
						type: 'default',
						message: "请输入正确的手机号"
					})
					return false;
				}

				if (this.$refs.uCode.canGetCode) {
					uni.removeStorageSync('codes');
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					// console.log(this.params.phone)
					apiccode(this.params.phone).then(res => {

						if (res.code === 200) {
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();

							this.params.code = res.data.code
							uni.setStorageSync('codes', this.params.code)
							// console.log(uni.getStorageSync('codes'))
						}
					})


				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	.page {
		position: relative;

		.img {
			z-index: -99999;
			position: absolute;
			top: 0rpx;
			width: 100vw;
			height: 100vh;
		}
	}

	.logo {

		text-align: center;

		image {
			margin: 80rpx 0rpx;
			width: 464rpx;
			height: 136rpx;

		}
	}

	.form {
		margin: 0rpx 36rpx;

		::v-deep .u-form-item__body {
			display: flex;
			background-color: #ffffff !important;
			margin-bottom: 10rpx;
			padding: 10rpx;
			height: 96rpx;
			border-radius: 16rpx;


		}
	}

	.but {
		margin: 50rpx;
		display: flex;
		font-size: 32rpx;
		font-weight: 600;

		.butphone {
			z-index: 100;
			position: relative;
		}

		.butcode {
			z-index: 100;
			position: relative;
		}

		.butcode::after {
			content: "";
			position: absolute;
			top: 20rpx;
			left: 10%;
			background-color: $theme-color;
			width: 80%;
			height: 40rpx;
			border-radius: 50rpx;
			z-index: -1;
		}

		.butphone::after {
			content: "";
			position: absolute;
			top: 20rpx;
			left: 10%;
			background-color: $theme-color;
			width: 80%;
			height: 40rpx;
			border-radius: 50rpx;
			z-index: -1;
		}
	}

	::v-deep .u-button {
		background-color: $theme-color !important;
	}

	::v-deep .u-form-item__body__left__content__label {
		font-size: 28rpx;
		padding-left: 20rpx;
	}

	.checkbox {
		margin: 120rpx 10rpx 60rpx 10rpx;
		text-align: center;
		font-size: 24rpx;
	}

	.amend {
		font-size: 24rpx;
		margin: 10rpx;
		@include flex(row, space-between);
	}
</style>